/*
 * ClientAlive.h
 *
 *  Created on: 08.12.2011
 *      Author: Pavel Sinay
 */

#ifndef CLIENTALIVE_H_
#define CLIENTALIVE_H_

#include "../IListener.h"
#include "../impl/SenderSocket.h"

namespace net {

class ClientAlive: public SenderSocket {
public:
	ClientAlive();
	virtual ~ClientAlive();

	void subscribe(IListener *listener);
	void connectServer(std::string const& host, unsigned short port);
	void closeConnection();
private:
	IListener *m_listener;

	pthread_t m_thread;
	pthread_attr_t m_attr;
	bool m_stop_req;
	bool m_thread_started;
	fd_set m_readset;

	std::string m_host;
	int m_port;

	/* from parent class*/
	void send(const std::string & host, unsigned short port,
			const std::vector<char> & tx_message, std::vector<char> & rx_message);

	static void *thread_operation(void *this_);
	void listening();

	void reestablishConnection();
};

}

#endif /* CLIENTALIVE_H_ */
